package com.yucc.girlimage.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.yucc.girlimage.R;
import com.yucc.girlimage.util.ImageWorker;

public class MyViewFlipper extends ViewFlipper {
	private ImageView mFirstImage ;
	private ImageView mMiddleImage ;
	private ImageView mLastImage ;
	private int mCurrentPosition ;
	private int mTotalCount ;

	public MyViewFlipper(Context context) {
		super(context);
		init() ;
	}

	public MyViewFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);
		init() ;
	}
	
	private void init(){
		LayoutInflater inflater =  LayoutInflater.from(getContext());
		
		View view = inflater.inflate(R.layout.fragment_image_detail, null) ;
		mFirstImage = (ImageView) view.findViewById(R.id.imageView) ;
		addView(view) ;
		view = inflater.inflate(R.layout.fragment_image_detail, null) ;
		mMiddleImage = (ImageView) view.findViewById(R.id.imageView) ;
		addView(view) ;
		view = inflater.inflate(R.layout.fragment_image_detail, null) ;
		mLastImage = (ImageView) view.findViewById(R.id.imageView) ;
		addView(view) ;
	}
	
	@Override
	public void showNext() {
		super.showNext();
		mCurrentPosition ++ ;
		if(mCurrentPosition > 1 && mCurrentPosition < mTotalCount - 1){
			ImageView temp = mFirstImage ;
			mFirstImage = mMiddleImage ;
			mMiddleImage = mLastImage ;
			mLastImage = temp ;
		}
	}
	
	@Override
	public void showPrevious() {
		super.showPrevious();
		mCurrentPosition -- ;
		if(mCurrentPosition > 0 && mCurrentPosition < mTotalCount - 2){
			ImageView temp = mFirstImage ;
			mFirstImage = mLastImage ;
			mLastImage = mMiddleImage ;
			mMiddleImage = temp ;
			mLastImage = temp ;
		}
		
	}
	
	public void initLoad(ImageWorker imageWorker,int currentPosition,int totalCount){
		mCurrentPosition = currentPosition ;
		mTotalCount = totalCount ;
		if(currentPosition > 0 && currentPosition < totalCount -1){
			imageWorker.loadImage(currentPosition -1, mFirstImage) ;
			imageWorker.loadImage(currentPosition, mMiddleImage) ;
			imageWorker.loadImage(currentPosition + 1, mLastImage) ;
		} else if(currentPosition == 0){
			imageWorker.loadImage(0, mFirstImage) ;
			imageWorker.loadImage(1, mMiddleImage) ;
		} else if(currentPosition == totalCount -1) {
			imageWorker.loadImage(currentPosition - 1, mMiddleImage) ;
			imageWorker.loadImage(currentPosition, mLastImage) ;
		}
	}
	
	public void loadLastImage(ImageWorker imageWorker){
		ImageWorker.cancelWork(mLastImage);
		mLastImage.setImageDrawable(null);
		imageWorker.loadImage(mCurrentPosition, mLastImage) ;
	}
	
	public void loadFirstImage(ImageWorker imageWorker){
		ImageWorker.cancelWork(mFirstImage);
		mFirstImage.setImageDrawable(null);
		imageWorker.loadImage(mCurrentPosition, mFirstImage) ;
	}
	
	public boolean hasPrevious(){
		return mCurrentPosition > 0 ? true : false ;
	}

	public boolean hasNext(){
		return mCurrentPosition < mTotalCount-1 ? true : false ;
	}
}
